package com.jiutong.meipenggang.http.present;

import android.content.Context;

import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.MCBMainPageBean;
import com.jiutong.meipenggang.entity.PayInBean;
import com.jiutong.meipenggang.entity.PayOut1Bean;
import com.jiutong.meipenggang.entity.PayOut2Bean;
import com.jiutong.meipenggang.entity.SurePayInBean;
import com.jiutong.meipenggang.entity.SurePayOut2Bean;
import com.jiutong.meipenggang.entity.SurePayOutBean;
import com.jiutong.meipenggang.http.model.interfaces.MeiCaiBaoBiz;
import com.jiutong.meipenggang.http.model.requests.MeiCaiBaoRequest;
import com.jiutong.meipenggang.http.subscribers.ProgressSubscriber;
import com.jiutong.meipenggang.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.utils.TimeUitl;

import java.util.HashMap;

/**
 * Created by bdf on 2018/1/4.
 */

public class MeiCaiBaoPresent {

    private final Context context;
    private final MeiCaiBaoBiz meiCaiBaoBiz;
    private final MeiCaiBaoRequest meiCaiBaoRequest;

    public MeiCaiBaoPresent(Context context, MeiCaiBaoBiz meiCaiBaoBiz) {
        this.context = context;
        this.meiCaiBaoBiz = meiCaiBaoBiz;
        meiCaiBaoRequest = new MeiCaiBaoRequest();
    }

    /**
     * 美财宝首页
     */

    public void mianPage() {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<MCBMainPageBean>() {
            @Override
            public void onNext(MCBMainPageBean subjects) {
                meiCaiBaoBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                meiCaiBaoBiz.onError(e);
            }
        };
        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT", 151);
        fieldsMap.put("timestamp", TimeUitl.getDate());
        fieldsMap.put("id", SpConfig.getInstance().getString(Constants.ID));
        fieldsMap.put("token", SpConfig.getInstance().getString(Constants.TOKEN));
        meiCaiBaoRequest.mcbMainPage(new ProgressSubscriber(onNextListener, context), fieldsMap);
    }

    /**
     * 美财宝余额转出
     */

    public void payout1() {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<PayOut1Bean>() {
            @Override
            public void onNext(PayOut1Bean subjects) {
                meiCaiBaoBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                meiCaiBaoBiz.onError(e);
            }
        };
        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT", 155);
        fieldsMap.put("timestamp", TimeUitl.getDate());
        fieldsMap.put("id", SpConfig.getInstance().getString(Constants.ID));
        fieldsMap.put("token", SpConfig.getInstance().getString(Constants.TOKEN));

        meiCaiBaoRequest.payout1(new ProgressSubscriber(onNextListener, context), fieldsMap);


    }

    /**
     * 美财宝匹配资产转出
     */
    public void payout2() {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<PayOut2Bean>() {
            @Override
            public void onNext(PayOut2Bean subjects) {
                meiCaiBaoBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                meiCaiBaoBiz.onError(e);
            }
        };
        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT", 154);
        fieldsMap.put("timestamp", TimeUitl.getDate());
        fieldsMap.put("id", SpConfig.getInstance().getString(Constants.ID));
        fieldsMap.put("token", SpConfig.getInstance().getString(Constants.TOKEN));

        meiCaiBaoRequest.payout2(new ProgressSubscriber(onNextListener, context), fieldsMap);
    }


    /**
     * 美财宝月确认转出
     *
     * @param amount 转出金额
     */
    public void surePayOut(String amount) {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<SurePayOutBean>() {
            @Override
            public void onNext(SurePayOutBean subjects) {
                meiCaiBaoBiz.onSuccess2(subjects);
            }

            @Override
            public void onError(Throwable e) {
                meiCaiBaoBiz.onError2(e);
            }
        };
        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT", 156);
        fieldsMap.put("timestamp", TimeUitl.getDate());
        fieldsMap.put("type", "1");
        fieldsMap.put("amount", amount);
        fieldsMap.put("id", SpConfig.getInstance().getString(Constants.ID));
        fieldsMap.put("token", SpConfig.getInstance().getString(Constants.TOKEN));

        meiCaiBaoRequest.surePayOut(new ProgressSubscriber(onNextListener, context), fieldsMap);

    }

    /**
     * 美财宝标的转出
     *
     * @param inverstId 标的id
     */

    public void surePayOut2(int inverstId) {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<SurePayOut2Bean>() {
            @Override
            public void onNext(SurePayOut2Bean subjects) {
                meiCaiBaoBiz.onSuccess2(subjects);
            }

            @Override
            public void onError(Throwable e) {
                meiCaiBaoBiz.onError2(e);
            }
        };
        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT", 156);
        fieldsMap.put("timestamp", TimeUitl.getDate());
        fieldsMap.put("type", "2");
        fieldsMap.put("investId", inverstId);
        fieldsMap.put("id", SpConfig.getInstance().getString(Constants.ID));
        fieldsMap.put("token", SpConfig.getInstance().getString(Constants.TOKEN));

        meiCaiBaoRequest.surePayOut2(new ProgressSubscriber(onNextListener, context), fieldsMap);
    }

    /**
     * 美财宝买入页面
     */

    public void payin() {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<PayInBean>() {
            @Override
            public void onNext(PayInBean subjects) {
                meiCaiBaoBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                meiCaiBaoBiz.onError(e);
            }
        };
        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT", 152);
        fieldsMap.put("timestamp", TimeUitl.getDate());
        fieldsMap.put("id", SpConfig.getInstance().getString(Constants.ID));
        fieldsMap.put("token", SpConfig.getInstance().getString(Constants.TOKEN));

        meiCaiBaoRequest.payin(new ProgressSubscriber(onNextListener, context), fieldsMap);

    }

    /**
     * 美财宝确认买入
     *
     * @param redid
     * @param price
     */

    public void surePayIn(String redid, String price) {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<SurePayInBean>() {
            @Override
            public void onNext(SurePayInBean subjects) {
                meiCaiBaoBiz.onSuccess2(subjects);
            }

            @Override
            public void onError(Throwable e) {
                meiCaiBaoBiz.onError2(e);
            }
        };
        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT", 153);
        fieldsMap.put("timestamp", TimeUitl.getDate());
        fieldsMap.put("redId", "123");
        fieldsMap.put("amount", price);
        fieldsMap.put("id", SpConfig.getInstance().getString(Constants.ID));
        fieldsMap.put("token", SpConfig.getInstance().getString(Constants.TOKEN));

        meiCaiBaoRequest.surepayin(new ProgressSubscriber(onNextListener, context), fieldsMap);
    }
}
